package com.zlx.exchange.goods.controller;


import com.zlx.exchange.domain.entity.Goods;
import com.zlx.exchange.goods.service.GoodsService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Objects;

/**
 * (Goods)表控制层
 *
 * @author makejava
 * @since 2023-09-04 19:23:36
 */
@RestController
@RequestMapping("api/goods")
public class GoodsController {
    /**
     * 服务对象
     */
    @Resource
    private GoodsService goodsService;

    /**
     * 分页查询
     *
     * @param goods   筛选条件
     * @param pageNum 查询页数
     * @param pageNum 查询总页数
     * @return 查询结果
     */
    @GetMapping("findAllGoods")
    public ResponseEntity<?> queryByPage(Goods goods, @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                         @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,Date timeBefore,Date timeAfter
    ) {
        if (Objects.isNull(goods)){goods=new Goods(null,null,null,null,null,null,null,null,null,new Date());}
        return this.goodsService.queryByPage(goods, pageNum, pageSize,timeBefore,timeAfter);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public ResponseEntity<?> queryById(@PathVariable("id") Integer id) {
        return this.goodsService.queryById(id);
    }

    /**
     * 新增数据
     *
     * @param goods 实体
     * @return 新增结果
     */
    @PostMapping
    public ResponseEntity<?> add(Goods goods) {
        return this.goodsService.insert(goods);
    }

    /**
     * 编辑数据
     *
     * @param goods 实体
     * @return 编辑结果
     */
    @PutMapping
    public ResponseEntity<?> edit(Goods goods) {
        return this.goodsService.update(goods);
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping
    public ResponseEntity<?> deleteById(Integer id) {
        return this.goodsService.deleteById(id);
    }

}

